package com.by.modules.controller;

import com.by.common.api.CommonResult;
import com.by.modules.dto.UmsAdminDto;
import com.by.modules.pojo.AdminLogin;
import com.by.modules.pojo.UmsAdmin;
import com.by.modules.service.IUmsAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.WebAuthenticationDetails;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

/**
 * description : 登录
 * author      : YuFei
 * data        : 2021/11/4 17:30
 */
@RestController
@Api(tags = "loginController")
/*@RequestMapping("/admin")*/
public class LoginController {
    @Autowired
    private IUmsAdminService adminService;
    @ApiOperation(value = "用户注册")
    @PostMapping("/register")
    public CommonResult register(@RequestBody UmsAdminDto dto) {
        CommonResult register = adminService.register(dto);
        return register;
    }
    @ApiOperation(value = "登录之后返回token")
    @PostMapping("/login")
    public CommonResult login(@RequestBody AdminLogin adminLogin, HttpServletRequest request) {
        return adminService.login(adminLogin.getUsername(),adminLogin.getPassword(),adminLogin.getCode(),request);
    }


    @ApiOperation(value = "获取当前登录的用户")
    @GetMapping("/info")
    public UmsAdmin getAdminInfo( Principal principal) {
        if (null == principal) {
            return null;
        }
        String username = principal.getName();
        UmsAdmin admin = adminService.getAdminByUsername(username);
        admin.setPassword(null);
        return admin;
    }
    @ApiOperation(value = "退出登录")
    @GetMapping("/logout")
    public CommonResult logout() {
        return CommonResult.success("注销成功");
    }
}
